package com.ikcai.deepcopy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
 * <p>标题：深浅拷贝例子 </p>
 * <p>功能： 深浅拷贝说明</p>
 * <p>所属模块： CUST</p>
 * <p>版权： Copyright © 2016 SNSOFT</p>
 * <p>公司: 北京南北天地科技股份有限公司</p>
 * <p>创建日期：2016年10月10日 上午9:53:13</p>
 * <p>类全名：com.ikcai.deepcopy.User</p>
 * 作者：赵力
 * 说明： 需要实现序列化接口Serializable才可以进行输入输出流的深拷贝
 * 初审：
 * 复审：
 * 监听使用界面:
 * @version 8.0
 */
public class User implements Serializable
{
	private String	name;
	private int		age;
	private String	email;
	public User		user;

	public static void main(String[] args) throws Exception
	{
		User user = new User("zhangsan", 12, "qq.com");
		User user1 = new User("zhangsan", 12, "qq.com", user);
		User user2 = copyUser(user1);
		User user3 = (User) DeepCopy.getDeepClone(user1);
		User user4 = DeepCopy.clone(user1);
		System.out.println("user1修改前：" + user1.user.toString());
		user.setAge(19);
		user.setEmail("163.com");
		System.out.println("user1修改后：" + user1.user.toString());
		System.out.println("user2浅拷贝：" + user2.user.toString());
		System.out.println("user3深拷贝：" + user3.user.toString());
		System.out.println("user3深拷贝：" + user4.user.toString());
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(user4);
		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		ObjectInputStream ois = new ObjectInputStream(bis);
		System.out.println(ois.readObject().toString());
		/************学习深拷贝到此结束！*****************/
	}

	public User()
	{
	}

	/**
	 * @param user
	 * @return
	 * 赵力
	 */
	private static User copyUser(User user)
	{
		User user1 = new User();
		user1.setName(user.getName());
		user1.setAge(user.getAge());
		user1.setEmail(user.getEmail());
		user1.setUser(user.getUser());
		return user1;
	}

	public User(String name, int age, String email)
	{
		this.name = name;
		this.age = age;
		this.email = email;
	}

	/**
	 * 
	 * @param name
	 * @param age
	 * @param email
	 * @param user
	 */
	public User(String name, int age, String email, User user)
	{
		this.name = name;
		this.age = age;
		this.email = email;
		this.user = user;
	}

	@Override
	public String toString()
	{
		return "name:" + this.name + ";age:" + this.age + ";email:" + this.email;
	}

	public String getName()
	{
		return name;
	}

	public int getAge()
	{
		return age;
	}

	public String getEmail()
	{
		return email;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setAge(int age)
	{
		this.age = age;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public User getUser()
	{
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}
}
